package com.woniuxy.hrp.mapper;

import com.woniuxy.hrp.entity.Department;
import com.woniuxy.hrp.entity.Hospital;
import com.woniuxy.hrp.provider.DepartmentSqlProvider;
import com.woniuxy.hrp.provider.HospitalMapperSqlProvider;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 科室mapper
 */
@Mapper
public interface DepartmentMapper {
    /**
     * 增加科室
     * @param department
     */
    @Insert("insert into hrp_department (name,hospital_id,description,fid,address) values(#{name},#{hospital.id},#{description},#{fid},#{address})")
    void add(Department department);

    /**
     * 根据科室id删除科室
     * @param id
     */
    @Delete("delete from hrp_department where id=#{id} ")
    void del(int id);

    //修改科室
    @UpdateProvider(type = DepartmentSqlProvider.class,method = "updateDepartmentByCondition")
    void updateByCondition(Department department);

    /**
     * 根据科室id查找科室
     * @param id
     * @return
     */
    @Select("select * from hrp_department where id=#{id}")
    @Results({
            @Result(column = "id", property = "id", id = true),
            @Result(column = "name",property = "name"),
            @Result(column = "description",property = "description"),
            @Result(column = "fid",property = "fid"),
            @Result(column = "address",property = "address"),
            @Result(column = "hospital_id",property = "hospital",one = @One(select = "com.woniuxy.hrp.mapper.HospitalMapper.findById"))
    })
    Department getById(int id);

    /**
     * 根据医院id查询科室
     * @param Hid
     * @return
     */
    @Select("select * from hrp_department where hospital_id=#{Hid}")
    List<Department> findByHospitalid(int Hid);

    /**
     * 查询所有的科室
     * @return
     */
    @Select("select * from hrp_department")
    List<Hospital> all();

    /***
     * @description: 根据医院id查所拥有的科室（一对多查询）
     * @param: patientId
     * @return: 指定医院拥有的科室集合
     * @author Lrw
     * @date: 2022/8/30 17:48
     */
    @Select("select d.* from hrp_hospital h ,hrp_department d where h.id=d.hospital_id and d.hospital_id=#{hospital.id}")
    List<Department> selectByHospitalId(int hospitalId);

    /***
     * @description: 根据一级科室查二级科室
     * @param: pid
     * @return: java.util.List<com.woniuxy.hrp.entity.Department>
     * @author Lrw
     * @date: 2022/9/4 0:35
     */
    @Select("select * from hrp_department where fid=#{pid}")
    List<Department> selectByPid(int pid);

    /***
     * @description: 查指定医院的所有一级科室
     * @return: java.util.List<com.woniuxy.hrp.entity.Department>
     * @author Lrw
     * @date: 2022/9/4 0:36
     */
    @Select("select * from hrp_department where fid is null and hospital_id=#{hospitalId}")
    List<Department> selectAllParentDepts(int hospitalId);


}
